package com.yksj.healthtalk.ui.chatting;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

import com.yksj.healthtalk.comm.BaseFragmentActivity;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.utils.FileUtils;
import com.yksj.healthtalk.utils.SystemUtils;

/**
 * 
 * 按名称或者多美号邀请
 * @author zhao
 *
 */
public class InviteByNameActivity extends BaseFragmentActivity implements OnClickListener{
	String groupId;
	ListView mListView;
	EditText mSearchEditText;
	Button mClearchBtn;
	DataAdapter mAdapter;
	private Button mSearchBtn;
	
	protected void onCreate(android.os.Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.invitebyname_layout);
		groupId = getIntent().getStringExtra("groupId");
		
		initView();
	};
	
	private void initView(){
		initTitle();
		
		setTitle("按昵称或多美号邀请");
		
		setBackBtn("返回",this);
		
		setRight("确定",this);
		
		mSearchBtn = (Button)findViewById(R.id.search_btn);
		mSearchBtn.setOnClickListener(this);
		mSearchEditText = (EditText)findViewById(R.id.search_edit);
		mListView  = (ListView)findViewById(android.R.id.list);
		mClearchBtn = (Button)findViewById(R.id.clear_btn);
		mClearchBtn.setOnClickListener(this);
		mAdapter = new DataAdapter(FileUtils.fatchSearchCacheList());
		mListView.setAdapter(mAdapter);
		findViewById(R.id.deleteText).setOnClickListener(this);
		if(mAdapter.mList.size() == 0){
			mClearchBtn.setVisibility(View.GONE);
		}
		
		mListView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				TextView textView  = (TextView)view;
				String name = textView.getText().toString();
				onSearchClick(name);
			}
		});
		
		mSearchEditText.addTextChangedListener(new TextWatcher() {
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				if(s.length()>0){
					mSearchBtn.setVisibility(View.VISIBLE);
					findViewById(R.id.deleteText).setVisibility(View.VISIBLE);
				}else{
					mSearchBtn.setVisibility(View.GONE);
					findViewById(R.id.deleteText).setVisibility(View.GONE);
				}
			}
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
			}
			@Override
			public void afterTextChanged(Editable s) {
				if(s.length() >0 ){
					titleRightBtn2.setClickable(true);
				}
				
			}
		});
		
		mSearchEditText.setOnEditorActionListener(new OnEditorActionListener() {
			
			@Override
			public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
				String txt = mSearchEditText.getEditableText().toString();
				mAdapter.addData(txt);
				onSearchClick(txt);
				return true;
			}
		});
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.title_back:
			SystemUtils.hideSoftBord(this, mSearchEditText);
			onBackPressed();
			break;
		case R.id.title_right2:
			String txt = mSearchEditText.getEditableText().toString();
			mAdapter.addData(txt);
			onSearchClick(txt);
			break;
		case R.id.deleteText:
			mSearchEditText.setText("");
			break;
		case R.id.search_btn:
			findViewById(R.id.deleteText).setVisibility(View.GONE);
			mSearchBtn.setVisibility(View.GONE);
			break;
		case R.id.clear_btn://清除
			mAdapter.mList.clear();
			mAdapter.notifyDataSetChanged();
			break;
		}
	}
	
	private void onSearchClick(String txt){
		Intent intent = new Intent(this,InviteFriendsListActivity.class);
		intent.putExtra("parame1",txt);
		intent.putExtra("groupId",groupId);
		startActivityForResult(intent,0);
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		FileUtils.updateSearchCacheList(mAdapter.mList);
	}
	
	class DataAdapter extends BaseAdapter{
		List<String> mList = new ArrayList<String>();
		private void addData(String str){
			if(!mList.contains(str)){
				mList.add(str);
				this.notifyDataSetChanged();
			}
			mClearchBtn.setVisibility(View.VISIBLE);
		}
		public DataAdapter(List<String> list) {
			this.mList = list;
		}
		@Override
		public int getCount() {
			return mList.size();
		}

		@Override
		public String getItem(int position) {
			return mList.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			final TextView textView;
			if(convertView == null){
				convertView = getLayoutInflater().inflate(R.layout.text_item_layout,null);
			}
			textView = (TextView)convertView;
			String txt = getItem(position);
			textView.setText(txt);
			return convertView;
		}
	}
}
